#!/bin/bash
#description: install gitlab-ce on centos7.3
#author: louis
#date: 2018/03/02
#执行脚本前，用root用户创建git用户并授权sudo
#useradd -u 1052 -d /var/opt/gitlab git
#echo "git ALL=(ALL) NOPASS: ALL" >>/etc/sudoer

VER="10.2.5"
DIR="/opt/tools"
EX_DOMAIN="git.91als.com"
GITDATA="/data/git-data"
MAIL="163.com"
MAILHOST="smtp.163.com"
MAILUSER="petgit@163.com"
MAILPASSWD="Az123321"
MAILPORT="25"

[ ! -d $DIR ] &&   sudo mkdir $DIR
[ ! -d $GITDATA ] && sudo mkdir $GITDATA
function init(){
	#安装依赖软件
	sudo yum -y install curl policycoreutils-python openssh-clients openssh-server postfix
	sudo systemctl enable postfix && sudo systemctl start postfix
	#配置防火墙
	#close selinux
	sudo sed -i 's@SELINUX=enforcing@SELINUX=disabled@g' /etc/selinux/config
	sudo setenforce 0
	#configure firewalld
	sudo  firewall-cmd --add-service=http
	sudo  firewall-cmd --add-service=http --permanent
	sudo  firewall-cmd --add-service=https
	sudo firewall-cmd --add-service=https --permanent
	sudo  firewall-cmd --add-port=25/tcp  --permanent
	sudo  firewall-cmd --reload
	sudo  systemctl restart firewalld
}
#安装gitlab-ce
function install_gitlab(){
	#安装gitlab
	cd $DIR && [ ! -f gitlab-ce-${VER}-ce.0.el7.x86_64.rpm ] && sudo  wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-${VER}-ce.0.el7.x86_64.rpm	
	sudo rpm -ivh gitlab-ce-${VER}-ce.0.el7.x86_64.rpm
	#启动gitlab
	sudo gitlab-ctl reconfigure
}
function configure_gitlab(){
	#汉化gitlab10.2.5
	yum -y install patch
	cd $DIR && sudo wget http://soft.51yuki.cn/${VER}-zh.diff
	sudo patch -d /opt/gitlab/embedded/service/gitlab-rails -p1 < ${VER}-zh.diff
	#配置external_url
	cd /etc/gitlab && [ ! -f gitlab.rb.ori ] && sudo cp gitlab.rb{,.ori}
	sudo sed -i "s@external_url 'http://gitlab.example.com'@external_url 'http://$EX_DOMAIN'@g" /etc/gitlab/gitlab.rb
	#更改仓库存储位置
	sudo chown -R git.git $GITDATA
	sudo chown -R git.git /etc/gitlab
	sudo echo 'git_data_dir "/data/git-data"' >> /etc/gitlab/gitlab.rb
	#配置邮箱通知
	sudo cat >>/etc/gitlab/gitlab.rb <<EOF
gitlab_rails['gitlab_email_enabled'] = true
gitlab_rails['gitlab_email_from'] = '${MAILUSER}'
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "${MAILHOST}"
gitlab_rails['smtp_port'] = ${MAILPORT}
gitlab_rails['smtp_user_name'] = "${MAILUSER}"
gitlab_rails['smtp_password'] = "${MAILPASSWD}"
gitlab_rails['smtp_domain'] = "${MAIL}"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = false
gitlab_rails['smtp_tls'] = false
user['git_user_email']="${MAILUSER}"
##############################################
gitlab_ci['smtp_enable'] = true
gitlab_ci['smtp_address'] = "${MAILPORT}"
gitlab_ci['smtp_port'] = ${MAILPORT}
gitlab_ci['smtp_user_name'] = "${MAILUSER}"
gitlab_ci['smtp_password'] = "${MAILPASSWD}"
gitlab_ci['smtp_domain'] = "${MAIL}"
gitlab_ci['smtp_authentication'] = "login"
gitlab_ci['smtp_enable_starttls_auto'] = false
###################################
EOF
	sudo gitlab-ctl stop
	sudo gitlab-ctl reconfigure
}
main() {
	init
	install_gitlab
	configure_gitlab
}
main
